const api = require('../../../api/store_api');
import { toast, log, err } from '../../../utils/wxapi';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    size: 20,
    list: [],
    user: {},
  },
  onLoad() {
    this.points();
  },

  async getuser() {
    try {
      const res = await api.getuser();
      if (res.respCode === 200) {
        const user = JSON.parse(res.source);
        this.setData({ user });
        wx.setStorageSync('user', user);
      }
    } catch (error) {
      err(error);
    }
  },
  async points() {
    try {
      const { page, size } = this.data;
      const res = await api.points({ page, size });
      if (res.data.respCode === 200) {
        let list = JSON.parse(res.source);
        log(list);
        if (this.data.list.length === 0) {
          this.setData({
            list,
            pageTotal: res.pageTotal,
          });
        } else {
          this.setData({
            list: this.data.list.concat(list),
            pageTotal: res.pageTotal,
          });
        }
      }
    } catch (error) {
      err(error);
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let { list, pageTotal, page } = this.data;
    if (list.length < pageTotal) {
      this.setData({
        page: page + 1,
      });
      this.points();
    } else {
      toast('已加载全部数据');
    }
  },
});
